package com.hengxinguotong.zhihuichengjian.ui.security;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.FirstFloorAdapter;
import com.hengxinguotong.zhihuichengjian.bean.SafetyBean;
import com.hengxinguotong.zhihuichengjian.bean.SafetyResultsRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.QualityQuestionActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/18.
 */

public class SafetyCheckResultsActivity extends BaseActivity implements View.OnClickListener,
        FirstFloorAdapter.OnExpandClickListener{

    @BindView(R.id.tv_back)
    HXTextView tvBack;
    @BindView(R.id.tv_title)
    HXTextView tvTitle;
    @BindView(R.id.tv_detail)
    HXTextView tvDetail;
    @BindView(R.id.results_lv)
    ExpandableListView resultsLv;

    private int type;
    private List<SafetyBean> mSafetyList;
    private List<SafetyBean> selectedDatas;
    private List<SafetyBean> selectedFirstDatas;
    private List<SafetyBean> selectedSecondDatas;
    private List<SafetyBean> selectedThirdDatas;
    private FirstFloorAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safety_check_results);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", Constants.SECURITY);//2施工安全检查 3日常安全检查
        getSafetyCheckResults("加载中…");
        resultsLv.setGroupIndicator(null);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(selectedDatas == null){
            selectedDatas = new ArrayList<SafetyBean>();
        }
        if(selectedFirstDatas == null){
            selectedFirstDatas = new ArrayList<SafetyBean>();
        }
        if(selectedSecondDatas == null){
            selectedSecondDatas = new ArrayList<SafetyBean>();
        }
        if(selectedThirdDatas == null){
            selectedThirdDatas = new ArrayList<SafetyBean>();
        }
        selectedDatas.clear();
    }

    private void getSafetyCheckResults(String alert) {
        RequestParams params = new RequestParams();
        List<SafetyBean> safetyBeanList = (List<SafetyBean>) getIntent().getSerializableExtra("safetyBeanList");
        for(int i=0; i<safetyBeanList.size(); i++) {
            params.addBodyParameter("itemsId[" + i + "]", safetyBeanList.get(i).getId());
        }
        Utils.requestData(alert, this, "/inspection/constructionSafe/querySafetyCheckResults/" + SPUtil.getString(this, "uuid"), params,
                new Utils.ListenerJson() {
                    @Override
                    public void onSuccess(String json) {
                        if (!Utils.isEmpty(json)) {
                            Gson gson = new Gson();
                            SafetyResultsRes safetyRes = gson.fromJson(json, SafetyResultsRes.class);
                            mSafetyList = safetyRes.getValue();
                            process();
                        }
                    }

                    @Override
                    public void onFailure(String json) {
                        showEmptyView(resultsLv);
                    }
                });
    }

    private void process(){
        mAdapter = new FirstFloorAdapter(this, mSafetyList);
        resultsLv.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnChildListener(this);
    }

    @OnClick({R.id.tv_back, R.id.tv_detail})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_detail:
                if(selectedThirdDatas.size() > 0){
                    for(SafetyBean bean1:selectedFirstDatas){
                        for(SafetyBean bean2:selectedSecondDatas){
                            if(!bean1.getData().contains(bean2)){
                                if(bean2.getParentId().equals(bean1.getId())){
                                    bean1.getData().add(bean2);
                                }
                            }
                        }
                    }
                    for(SafetyBean bean1:selectedSecondDatas){
                        for(SafetyBean bean2:selectedThirdDatas){
                            if(!bean1.getData().contains(bean2)) {
                                if (bean2.getParentId().equals(bean1.getId())) {
                                    bean1.getData().add(bean2);
                                }
                            }
                        }
                    }
                    selectedDatas.addAll(selectedFirstDatas);
                    Intent intent = new Intent();
                    if(type == Constants.SECURITY){//2施工安全检查 3日常安全检查
                        intent.setClass(this, SecurityTestActivity.class);
                    }else if(type == Constants.DAILY_SECURITY){
                        intent.setClass(this, DailySecurityTestActivity.class);
                    }else if(type == Constants.QUALITY_QUESTION){
                        intent.setClass(this, QualityQuestionActivity.class);
                    }else if(type == Constants.SECURITY_QUESTION){
                        intent.setClass(this, SecurityQuestionActivity.class);
                    }
                    intent.putExtra("selectedDatas", (Serializable) selectedDatas);
                    intent.putExtra("selectedFirstDatas", (Serializable) selectedFirstDatas);
                    intent.putExtra("selectedThirdDatas", (Serializable) selectedThirdDatas);
                    startActivity(intent);
                }else{
                    showToast("请至少选择一项");
                }
                break;
        }
    }

    public List<SafetyBean> getmSelectedSafetyList() {
        return selectedDatas;
    }

    @Override
    public void onclick(int parentPosition, int childPosition, int childIndex) throws CloneNotSupportedException {
        try {
            SafetyBean safetyBean = mSafetyList.get(parentPosition).getData().get(childPosition).getData().get(childIndex);
            SafetyBean firstBean = (SafetyBean) mSafetyList.get(parentPosition).clone();
            SafetyBean secondBean = (SafetyBean) firstBean.getData().get(childPosition).clone();
            SafetyBean thirdBean = (SafetyBean) secondBean.getData().get(childIndex).clone();
            if(selectedThirdDatas.contains(thirdBean)){
                safetyBean.setSelected(false);
                selectedThirdDatas.remove(thirdBean);
            }else{
                safetyBean.setSelected(true);
                selectedThirdDatas.add(thirdBean);
            }
            if(selectedSecondDatas.contains(secondBean)){
//            selectedSecondDatas.remove(secondBean);
            }else{
                secondBean.getData().clear();
                selectedSecondDatas.add(secondBean);
            }
            if(selectedFirstDatas.contains(firstBean)){
//            selectedFirstDatas.remove(firstBean);
            }else{
                firstBean.getData().clear();
                selectedFirstDatas.add(firstBean);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

}
